const db = require("../../config/db");
const md5 = require("md5");
const myRondom = "&*^^%&**##$%$";

let { HOST_URL } = require("../../config/host");
let user_avators = ["/img/1.jpg", "/img/2.jpg", "/img/3.jpg", "/img/4.jpg", "/img/5.jpg", "/img/6.jpg"];
let rd = () => {
    return Math.floor(Math.random() * user_avators.length);
};
const JwtUtil = require("../../utils/jwt");

const register = (user_acc, user_pwd, user_nick_name, create_time, email) => {
    // console.log(email, 666);
    // console.log(email);
    return new Promise((resolve, reject) => {
        let code_sql = 'select * from news_code where code_pwd="' + email + '"';
        console.log(code_sql, 777);
        db.query(code_sql, (error, result1) => {
            if (error) {
                reject({
                    code: -1,
                    msg: "查询数据库出错",
                    error,
                });
            } else {
                console.log(result1);
                if (result1.length != 0) {
                    if (result1[0].code_pwd == email) {
                        let user_check_sql = `select * from news_user where user_acc = "${user_acc}"`;
                        db.query(user_check_sql, (error, result) => {
                            if (error) {
                                reject({
                                    code: -1,
                                    msg: "查询用户异常。",
                                    error,
                                });
                            } else {
                                // result: 数组
                                // result.length == 0 该账号还没被注册
                                if (result.length == 0) {
                                    let avator = HOST_URL + user_avators[rd()];
                                    let user_insert_sql = `insert into news_user(user_acc,user_pwd,user_nick_name,create_time,user_img) values("${user_acc}","${md5(
                                        md5(user_pwd) + myRondom
                                    )}","${user_nick_name}","${create_time}","${avator}")`;
                                    db.query(user_insert_sql, (err, rows) => {
                                        if (err) {
                                            reject({
                                                code: -1,
                                                msg: "用户注册失败。",
                                                err,
                                            });
                                        } else {
                                            resolve({
                                                code: 200,
                                                msg: "用户注册成功。",
                                            });
                                        }
                                    });
                                } else {
                                    resolve({
                                        code: 201,
                                        msg: "该账号已经存在。",
                                    });
                                }
                            }
                        });
                    } else {
                        reject({
                            code: -1,
                            msg: "验证码错误",
                            error,
                        });
                    }
                } else {
                    reject({
                        code: -1,
                        msg: "验证码错误",
                        error,
                    });
                }
            }
        });
    });
};
const login = (user_acc, user_pwd, req) => {
    return new Promise((resolve, reject) => {
        let user_select_sql = `select * from news_user where user_acc = "${user_acc}"`;
        db.query(user_select_sql, (error, result) => {
            if (error) {
                reject({
                    code: -1,
                    msg: "查询用户异常。",
                    error,
                });
            } else {
                if (result.length == 0) {
                    console.log(46545, result.length);
                    reject({
                        code: 201,
                        msg: "该账号未注册。",
                    });
                } else {
                    let user_select_pwd = `select user_pwd,user_id from news_user where user_acc = "${user_acc}"`;
                    db.query(user_select_pwd, (err, res) => {
                        if (res[0].user_pwd != md5(md5(user_pwd) + myRondom)) {
                            reject({
                                code: -1,
                                msg: "用户名或密码错误。",
                                err,
                            });
                        } else {
                            let jwt = new JwtUtil(res[0].user_id);
                            let token = jwt.generateToken();
                            console.log(token);
                            resolve({
                                code: 200,
                                res,
                                msg: "用户登录成功。",
                                token,
                            });
                        }
                    });
                }
            }
        });
    });
};
const selectTokenUser = (x) => {
    return new Promise((resolve, reject) => {
        let token_user_sql = `select * from news_user where user_id="${x}"`;
        db.query(token_user_sql, (error, result) => {
            if (error) {
                reject({
                    code: -1,
                    error,
                });
            } else {
                if (result.length > 0) {
                    resolve({
                        code: 200,
                        result,
                    });
                }
            }
        });
    });
};
const modifyMsg = (user_id, user_pwd, user_signature, user_phone, user_nick_name) => {
    return new Promise((resolve, reject) => {
        let sql = `select * from news_user where user_id="${user_id}"`;
        console.log(1212121);

        db.query(sql, (error, result) => {
            if (error) {
                reject({
                    code: -1,
                    error,
                    msg: "查询数据库失败",
                });
            } else {
                let updata_sql = `update news_user set user_pwd= "${md5(
                    md5(user_pwd) + myRondom
                )}",user_signature= "${user_signature}",user_phone= "${user_phone}",user_nick_name= "${user_nick_name}" where user_id= "${user_id}"`;
                console.log(updata_sql, 123);

                db.query(updata_sql, (err, rows) => {
                    if (err) {
                        reject({ code: -1, err, msg: "更新失败" });
                    } else {
                        console.log(rows);
                        resolve({
                            code: 200,
                            rows,
                            msg: "更新成功",
                        });
                    }
                });
            }
        });
    });
};
module.exports = {
    register(user_acc, user_pwd, user_nick_name, create_time, email) {
        // 以后需要用到邮箱验证码
        // To do ....
        return register(user_acc, user_pwd, user_nick_name, create_time, email);
    },
    login(user_acc, user_pwd, req) {
        return login(user_acc, user_pwd, req);
    },
    selectTokenUser(x) {
        return selectTokenUser(x);
    },
    modify_avator(user_id, user_img) {
        return new Promise((resolve, reject) => {
            let usr_check_sql = `select * from news_user where user_id = "${user_id}"`;
            db.query(usr_check_sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "查询用户失败",
                        err,
                    });
                } else {
                    if (rows.length == 1) {
                        let user_modify_sql = `update news_user set user_img= "${user_img}" where user_id= "${user_id}"`;
                        db.query(user_modify_sql, (error, result) => {
                            // console.log("9.0", { user_id, user_img, error });
                            if (error) {
                                reject({
                                    code: -1,
                                    msg: "修改用户头像失败。",
                                    error,
                                });
                            } else {
                                resolve({
                                    code: 200,
                                    msg: "修改用户头像成功。",
                                    user_img,
                                });
                            }
                        });
                    }
                }
            });
        });
    },
    modifyMsg(user_id, user_pwd, user_signature, user_phone, user_nick_name) {
        return modifyMsg(user_id, user_pwd, user_signature, user_phone, user_nick_name);
    },
};
